package com.ys.service.sys.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bo.dto.NameValueSelectDto;
import com.bo.util.DateUtil;
import com.ys.dto.AddDto;
import com.ys.dto.DtreeSqlDto;
import com.ys.entity.flow.YspModel;
import com.ys.entity.sys.YsGrid;
import com.ys.entity.sys.YsMenu;
import com.ys.entity.sys.YsMenuNo;
import com.ys.enume.sys.YsMenuEnum;
import com.ys.mapper.sys.SysMapper;
import com.ys.mapper.sys.YsGridMapper;
import com.ys.mapper.sys.YsMenuMapper;
import com.ys.mapper.sys.YsMenuNoMapper;
import com.ys.service.sys.YsGridService;
import com.ys.vo.R;
import com.ys.vo.W;

@Service("ysGridService")
public class YsGridServiceImpl implements YsGridService {

	@Autowired
	private SysMapper sysMapper;
	
	@Autowired
	private YsMenuMapper ysMenuMapper;
	@Autowired
	private YsGridMapper ysGridMapper;
	@Autowired
	private YsMenuNoMapper ysMenuNoMapper;

	@Override
	public DtreeSqlDto dtree() {
		
		DtreeSqlDto.StatusDto status = DtreeSqlDto.StatusDto.init(200, "");

		List<DtreeSqlDto.DataDto> data = new ArrayList<DtreeSqlDto.DataDto>();
		List<YsGrid> listEntity = new YsGrid()
				.queryList();
		
		// 一级菜单
		for(YsGrid entity : listEntity) {
			String id1 = entity.getId().toString();
			String title1 = entity.getName();
			String parentId1 = null;
			
			// 二级菜单
			List<DtreeSqlDto.DataDto> children = new ArrayList<DtreeSqlDto.DataDto>();
			
			DtreeSqlDto.DataDto dataDto1 = new DtreeSqlDto.DataDto(id1, title1, parentId1, DtreeSqlDto.DataDto.initCheckArr(null), children);
			data.add(dataDto1);
		}
		return new DtreeSqlDto(status, data);
	}

	@Override
	@Transactional
	public R deleteTrue(Long id) {
		if (id == null) {
			return R.error("ID不能为空");
		}
		
//		// 删除角色-按钮
//		sysMapper.deleteTrueSysRoleButtonByTsDictionaryForModuleId(id);
//		
//		// 删除按钮
//		tsDictionaryForModuleMapper.deleteTrue(id);
		
		return R.ok();
	}
	
	@Override
	public R add(Map<String, Object> params) {
		
		YsGrid entity = JSON.parseObject(JSON.toJSONString(params), YsGrid.class);
		
		ysGridMapper.insert(entity);
		return R.ok(AddDto.closeAndReload());
	}
	
	@Override
	public R edit(Map<String, Object> params) {

		YsMenu entity = JSON.parseObject(JSON.toJSONString(params), YsMenu.class);
		
		String templetType = entity.getTempletType();
		if (templetType.equals(YsMenuEnum.TempletType.LIST.value) || templetType.equals(YsMenuEnum.TempletType.TREE_WITH_LIST.value)) {
			entity.setTempletPath("sys/common/templet_list");
		}
		
		ysMenuMapper.update(entity);
		return R.ok("操作成功");
	}

	@Override
	public R move(Long id, Long pid) {

		YsMenu entity = new YsMenu().setId(id).queryFirst();
		
		YsMenu pEntity = new YsMenu().setId(pid).queryFirst();
		if (entity.getLev() - 1 != pEntity.getLev()) {
			return R.error("操作失败");
		}
		entity.setPid(pid);
		
		ysMenuMapper.update(entity);
		return R.ok("操作成功");
	}

	@Override
	public String toResort(Model model, Long pid) {
		
		if (pid == null) {
			return W.error(model, "菜单ID不能为空");
		}
		
		List<YsMenu> listYsMenu = new YsMenu().setPid(pid).setOrderby("A.SortNo asc").queryList();
		
		List<ToResortDto.RowDto> rows = new ArrayList<ToResortDto.RowDto>();
		for(YsMenu entity : listYsMenu) {
			Long id = entity.getId();
			String name = entity.getName();
			rows.add(new ToResortDto.RowDto(id, name));
		}
		
		ToResortDto dto = new ToResortDto(rows);
		model.addAttribute("dto", dto);
		return "sys/ysMenu/toResort";
	}

	@Override
	@Transactional
	public R resort(String rows) {
		JSONArray JA = JSON.parseArray(rows);
		
		for (int i = 0; i < JA.size(); i++) {
			JSONObject JO = JA.getJSONObject(i);
			Long id = JO.getLong("id");
			Integer sortNo = i + 1;
			sysMapper.updateYsMenuOfSortNo(id, sortNo);
		}
		return R.ok();
	}

	@Override
	public String toSetting(Model model, Long id) {
		if (id == null) {
			return W.error(model, "ID不能为空");
		}
		
		YsMenu entity = new YsMenu().setId(id).queryFirst();
		
		String name = entity.getName();
		String nameShiroAuth = entity.getNameShiroAuth();
		
		String templetType = entity.getTempletType();
		String pathGridRequest = entity.getPathGridRequest();
		String pathGridEditRequest = entity.getPathGridEditRequest();
		String pathGridExportRequest = entity.getPathGridExportRequest();
		String pathCommitAdd = entity.getPathCommitAdd();
		String pathCommitEdit = entity.getPathCommitEdit();
		String pathCommitDelete = entity.getPathCommitDelete();
		String pathFlowPass = entity.getPathFlowPass();
		String pathFlowRefuse = entity.getPathFlowRefuse();
		
		String dataBaseTable = entity.getDataBaseTable();
		String pathSelfDefine = entity.getTempletPath();
		pathGridRequest = pathGridRequest == null || pathGridRequest.length() == 0 ? "/api/inter/" + com.ys.util.StringUtil.parseTableName(dataBaseTable) + "/search" : pathGridRequest;
		pathGridEditRequest = pathGridEditRequest == null || pathGridEditRequest.length() == 0 ? "/api/inter/common/editOnTable?menuId=" + entity.getId() : pathGridEditRequest;
		pathGridExportRequest = pathGridExportRequest == null || pathGridExportRequest.length() == 0 ? "/api/inter/" + com.ys.util.StringUtil.parseTableName(dataBaseTable) + "/export" : pathGridExportRequest;

		ysMenuMapper.update(entity);

		
		List<NameValueSelectDto> templetTypeArr = new ArrayList<NameValueSelectDto>();
		for (YsMenuEnum.TempletType e : YsMenuEnum.TempletType.values()) {
			templetTypeArr.add(new NameValueSelectDto(e.name, e.value.toString(), e.value.equals(templetType)));
		}
		
		// 流程数组
		Long flowModelId = entity.getFlowModelId();
		List<NameValueSelectDto> flowModelArr = new ArrayList<NameValueSelectDto>();
		List<YspModel> listYspModel = new YspModel().queryList();
		{
			flowModelArr.add(new NameValueSelectDto("暂未设置", "0", flowModelId.longValue() == 0L));
		}
		for(YspModel yspModel : listYspModel) {
			String name2 = yspModel.getName() + "(" + yspModel.getCode() + ")";
			String value = yspModel.getId().toString();
			boolean isSelected = flowModelId != null && flowModelId.longValue() == yspModel.getId().longValue() ? true : false;
			flowModelArr.add(new NameValueSelectDto(name2, value, isSelected));
		}
		
		YsMenuNo ysMenuNo = new YsMenuNo().setMenuId(id).queryFirst();
		String preCode = null; // 单号头
		String connectCode = null; // 连接符
		Integer startNo = null; // 起始号
		if (ysMenuNo != null) {
			preCode = ysMenuNo.getPreCode();
			connectCode = ysMenuNo.getConnectCode();
			startNo = ysMenuNo.getStartNo();
		}
		
		
		ToSettingDto dto = new ToSettingDto(id, name, nameShiroAuth, templetTypeArr, pathSelfDefine, pathGridRequest, pathGridEditRequest, pathGridExportRequest, pathCommitAdd, pathCommitEdit, pathCommitDelete, flowModelArr, pathFlowPass, pathFlowRefuse,
				preCode, connectCode, startNo);
		model.addAttribute("dto", dto);
		return "sys/ysMenu/toSetting";
	}

	@Override
	public R setting(Long id,
			String nameShiroAuth,
			String templetType,
			String pathSelfDefine,
			String pathGridRequest, String pathGridEditRequest, String pathGridExportRequest,
			String pathCommitAdd, String pathCommitEdit, String pathCommitDelete,
			Long flowModelId, String pathFlowPass, String pathFlowRefuse,
			String preCode, String connectCode, Integer startNo) {
		
		YsMenu entity = new YsMenu().setId(id).queryFirst();
		if (entity == null) {
			return R.error();
		}
		
		// 自定义时，必须配置相关地址
		else if (YsMenuEnum.TempletType.SELF_DEFINE.value.equals(templetType)) {
			if (pathSelfDefine == null || pathSelfDefine.length() == 0) {
				return R.error("请填写自定义地址");
			}
		}
		
		// 模板地址
		String templetPath = "";
		if (YsMenuEnum.TempletType.LIST.value.equals(templetType)) {
			templetPath = "sys/common/templet_list";
		} else if (YsMenuEnum.TempletType.TREE_WITH_LIST.value.equals(templetType)) {
			templetPath = "sys/common/templet_tree_with_list_normal";
		} else if (YsMenuEnum.TempletType.SELF_DEFINE.value.equals(templetType)) {
			templetPath = pathSelfDefine;
		}

		entity
			.setNameShiroAuth(nameShiroAuth)
			.setTempletType(templetType)
			.setTempletPath(templetPath)
			.setPathGridRequest(pathGridRequest)
			.setPathGridEditRequest(pathGridEditRequest)
			.setPathGridExportRequest(pathGridExportRequest)
			.setPathCommitAdd(pathCommitAdd)
			.setPathCommitEdit(pathCommitEdit)
			.setPathCommitDelete(pathCommitDelete)
			.setFlowModelId(flowModelId)
			.setPathFlowPass(pathFlowPass)
			.setPathFlowRefuse(pathFlowRefuse);
		ysMenuMapper.update(entity);
		
		if (startNo != null && startNo > 0) {
			YsMenuNo ysMenuNo = new YsMenuNo().setMenuId(id).queryFirst();
			if (ysMenuNo == null) {
				Long menuId = id;
				Integer currentNo = startNo;
				String currentDay = DateUtil.getYYYY_MM_DD(new Date());
				ysMenuNo = YsMenuNo.ysMenuNo(menuId, preCode, connectCode, startNo, currentNo, currentDay, 1);
				ysMenuNoMapper.insert(ysMenuNo);
			} else {
				ysMenuNo.setPreCode(preCode)
					.setConnectCode(connectCode)
					.setStartNo(startNo)
					.setCurrentNo(startNo);
				ysMenuNoMapper.update(ysMenuNo);
			}
		}
		return R.ok("操作成功");
	}
}
